package com.xbj.controller;

import com.xbj.model.User;
import com.xbj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpSession;

/**
 * Created by JzzZ
 * Date: 2018\7\9 0009
 * Time: 15:45
 * Project: News2.0
 * File description:
 */
@Controller
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public String login(){
        return "admin/jsp/login";
    }

    @RequestMapping("/checkLogin")
    public String checkLogin(User user, Model model, HttpSession session){
//        数据校验
        String userName = user.getUsername();
        String password = user.getPassword();
        if (userName == null || userName == "".trim()) {
            model.addAttribute("spanName", "用户名不能为空");
        }
        if (password == null || password == "".trim()) {
            model.addAttribute("spanPassWord", "密码不能为空");
        }

        User checkUser = userService.login(user);
        model.addAttribute("user",checkUser);
        session.setAttribute("NEWS_SESSION_TUSER",checkUser);

        return "redirect:/menu/main";
    }
    @RequestMapping("/quit")
    public String quit(HttpSession session){
        session.invalidate();
        return "redirect:/login";
    }

}
